require(['./config'], () => {
    require(['template', 'bootstrap', 'distdata', 'dist', 'header', 'footer', 'toTop'], (template) => {
        class Cart {
            constructor() {
                this.init()
                this.calcCartNum()
                this.address()
                this.addAddress()
                this.delAddress()
                this.random()
                this.toTop()
            }
            init() {
                let cartList = localStorage.getItem('cart')
                if (cartList) {
                    this.cartList = JSON.parse(cartList)
                    $('#cartPay').html(template('cartShop', { cartList: this.cartList }))
                        // 有数据的话就要默认计算一次总价
                    this.calcTotalMoney()
                } else {
                    // 购物车没有数据
                    $('#cart-empty').show()
                }
            }
            calcTotalMoney() {
                $('.money').html(this.cartList.reduce((money, cart) => {
                    money += cart.price * cart.num
                    return money
                }, 0).toFixed(2))
            }
            calcCartNum() {
                    let cartList = localStorage.getItem('cart')
                    cartList = JSON.parse(cartList)
                    let num = cartList.reduce((total, cart) => {
                        total += cart.num
                        return total;
                    }, 0)
                    $('#shopnum').html(num)

                }
                // 模态框
            mapShow() {
                $('#target').distpicker({
                    province: '---- 省 ----',
                    city: '---- 市 ----',
                    district: '---- 区 ----'
                });
            }
            address() {
                const _this = this
                $('.save').on('click', function() {
                    let addrlist = localStorage.getItem('address')
                    if (addrlist) {
                        addrlist = JSON.parse(addrlist)
                        const isExist = addrlist.some(address => {
                            return address.username === $('#username').val()
                        })
                        if (isExist) {
                            addrlist = addrlist.map(address => {
                                if (address.username === $('#username').val()) {

                                }
                                return address
                            })
                        } else {
                            // 没有当前地址
                            addrlist.push({
                                username: $('#username').val(),
                                phone: $('#phone').val(),
                                pro: $('#pro').val(),
                                city: $('#city').val(),
                                town: $('#town').val(),
                                addr: $('#addr').val(),
                                townCode: $('#townCode').val(),
                            })
                        }
                        localStorage.setItem('address', JSON.stringify(addrlist))
                    } else {
                        localStorage.setItem('address', JSON.stringify([{
                            username: $('#username').val(),
                            phone: $('#phone').val(),
                            pro: $('#pro').val(),
                            city: $('#city').val(),
                            town: $('#town').val(),
                            addr: $('#addr').val(),
                            townCode: $('#townCode').val(),
                        }]))
                    }
                    _this.addAddress()
                })
            }
            addAddress() {
                let addrlist = localStorage.getItem('address')
                addrlist = JSON.parse(addrlist)
                $('#addr-list').html(template('addrInfo', { addrlist }))

            }
            delAddress() {
                $('.addr-del').on('click', function() {
                    let thisUsername = $($(this).parents('li').children()[0]).html()
                    let addlist = localStorage.getItem('address')
                    if (addlist) {
                        addlist = JSON.parse(addlist)
                        if (confirm('确定要删除吗?')) {
                            addlist = addlist.filter(addr => {
                                return addr.username != thisUsername
                            })
                            $(this).parents('li').remove()
                        }
                        console.log(addlist)
                        localStorage.setItem('address', JSON.stringify(addlist))
                    }
                })

            }
            random() {
                $('.dd-btn').on('click', 'button', function() {
                    var strnum = ''
                    var strword = ''
                    var arrnum = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
                    var arrword = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
                        'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
                    ]
                    for (var i = 0; i < 18; i++) {
                        var pos = Math.round(Math.random() * (arrnum.length - 1))
                        strnum += arrnum[pos]
                    }
                    for (var i = 0; i < 2; i++) {
                        var pos = Math.round(Math.random() * (arrnum.length - 1))
                        strword += arrword[pos]
                    }
                    $('#sj').html(strword + strnum)
                })
            }
            toTop() {
                $('.totop').toTop();
            }
        }
        new Cart()
    })
})